﻿
using UnityEngine;

namespace XFGameFramework.TweenSystem
{

    /// <summary>
    /// SpriteRenderer动画拓展
    /// </summary>
    public static class SpriteRendererExtensions
    {
        /// <summary>
        /// 渐变颜色
        /// </summary>
        /// <param name="spriteRenderer">SpriteRenderer对象</param>
        /// <param name="target">目标颜色</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<Color> TweenColor(this SpriteRenderer spriteRenderer, Color target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        {  
            return TweenManager.ComponentTween(spriteRenderer, spriteRenderer.color, target, duration, loop, ease, curve, ingoreTimeScale, OnTweenColor, delay,0,loopCount);
        }

        internal static void OnTweenColor(TweenerCore<Color> tweener)
        {
            if (tweener.Component is SpriteRenderer)
            {
                SpriteRenderer spriteRenderer = tweener.Component as SpriteRenderer;
                spriteRenderer.color = tweener.Value;
            }
        }

        /// <summary>
        /// 渐变透明度
        /// </summary>
        /// <param name="spriteRenderer">SpriteRenderer对象</param>
        /// <param name="target">目标透明度</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<float> TweenFade(this SpriteRenderer spriteRenderer, float target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        {
            return TweenManager.ComponentTween(spriteRenderer, spriteRenderer.color.a, target, duration, loop, ease, curve, ingoreTimeScale, OnTweenFade, delay,0,loopCount);
        }

        internal static void OnTweenFade(TweenerCore<float> tweener)
        {
            if (tweener.Component is SpriteRenderer)
            {
                SpriteRenderer spriteRenderer = tweener.Component as SpriteRenderer;
                spriteRenderer.color = new Color(spriteRenderer.color.r, spriteRenderer.color.g, spriteRenderer.color.b, tweener.Value);
            }
        }

        /// <summary>
        /// 渐变Size
        /// </summary>
        /// <param name="spriteRenderer">SpriteRenderer对象</param>
        /// <param name="target">目标值</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<Vector2> TweenSize(this SpriteRenderer spriteRenderer, Vector2 target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        { 
            return TweenManager.ComponentTween(spriteRenderer, spriteRenderer.size, target, duration, loop, ease, curve, ingoreTimeScale, OnTweenSize, delay, 0, loopCount);
        }


        internal static void OnTweenSize(TweenerCore<Vector2> tweener)
        {
            if (tweener.Component is SpriteRenderer)
            {
                SpriteRenderer spriteRenderer = tweener.Component as SpriteRenderer;
                spriteRenderer.size = tweener.Value;
            }
        }

    }

}

